<?php
class ListdocumentForm extends Form
{
	function __construct()
	{
		Form::__construct('ListdocumentForm');
	}

	function draw()
	{
		global $display;
		$this->beginForm(false,"POST",false,Url::build_current());
		
		$display->add('msg',$this->showFormErrorMessages(1));
		$display->add('url_admin',Url::build_current());

		$key_word = Url::get('key_word');
		$display->add('key_word',			$key_word);
		
		$display->add('option_date1',		AZLib::getOptionNum(1,31,(int)Url::get('cmb_date1',1)));
		$display->add('option_month1',		AZLib::getOptionNum(1,12,(int)Url::get('cmb_month1',1)));
		$display->add('option_year1',		AZLib::getOptionNum(2011,date("Y"),(int)Url::get('cmb_year1',2011)));
		$display->add('option_date2',		AZLib::getOptionNum(1,31,(int)Url::get('cmb_date2',date('d'))));
		$display->add('option_month2',		AZLib::getOptionNum(1,12,(int)Url::get('cmb_month2',date('m'))));
		$display->add('option_year2',		AZLib::getOptionNum(2011,date("Y"),(int)Url::get('cmb_year2',date('Y'))));
		
		$display->add('url_add',Url::build_current(array('cmd'=>'add')));
		
		if ( (Url::get('cmb_date1')!='') && (Url::get('cmb_month1')!='') && (Url::get('cmb_year1')!='') )
		{
			$from_date = mktime(0,0,0,Url::get('cmb_month1'),Url::get('cmb_date1'),Url::get('cmb_year1'));
		}
		else{
			$from_date = 0;
		}
		
		if ( (Url::get('cmb_date2')!='') && (Url::get('cmb_month2')!='') && (Url::get('cmb_year2')!='') )
		{
			$to_date = mktime(23,59,59,Url::get('cmb_month2'),Url::get('cmb_date2'),Url::get('cmb_year2'));
		}
		else{
			$to_date = 0;
		}
		
		$condition	=	'';
		$status		=	Url::get('status',0);
		$user_c		=	Url::get('user_c');
		$user_m		=	Url::get('user_m');
		
		
		
		if($status==1)
			$condition.=($condition!=''?' AND ':' ').' status=1';
		elseif($status==2)
			$condition.=($condition!=''?' AND ':' ').' status=0';
			
		if($user_c!='')
			$condition.=($condition!=''?' AND ':' ').' user_c LIKE "%'.$user_c.'%"';
		if($user_m!='')
			$condition.=($condition!=''?' AND ':' ').' user_m LIKE "%'.$user_m.'%"';
			
		if($from_date&&$to_date)
		$condition.=($condition!=''?' AND ':' ').' time_c >='.$from_date.' AND time_c <= '.$to_date;
			
		$display->add('user_c',$user_c);
		$display->add('user_m',$user_m);
		$display->add('status_option',AZLib::getOption(array(0=>"Tất cả",1=>"Hiện",2=>"Ẩn SP"),$status));

		if($key_word)
		{
			$key_word_s = '';
			
			$arr = explode(' ',$key_word);
			
			if($arr)
			{
				foreach ($arr as $key)
				{
					$key = trim($key);
					
					if($key != '')
					{
						$key_word_s .= ($key_word_s==''?'+':' +')."$key";
					}
				}
			}
			
			if($key_word_s != '')
			{
				$condition .=" AND MATCH(title, des) AGAINST ('$key_word_s' IN BOOLEAN MODE)";
			}
			
			//$condition .= ' AND (title LIKE "%'.$key_word.'%" OR des LIKE "%'.$key_word.'%")';
		}
		
		/*if(Url::get('key_word','str',''))
		$condition.=($condition!=''?' AND ':' ').' (title LIKE "%'.$key_word.'%" OR des LIKE "%'.$key_word.'%")';*/
		
		$total_item	=	DB::count('document',$condition);
			
		$LIMIT='';
		$paging	=	AZPagging::paging_list($LIMIT,$total_item,20,10,'page_no','',true,"Tài liệu",'Trang');
		
		$display->add('paging',$paging);
		
		$condition = ($condition!=''?'WHERE':'').$condition;
		
		$documents	=	array();
		
		$sql	=	'SELECT * FROM document '.$condition.' ORDER BY id DESC '.$LIMIT;
		$re = DB::query($sql);
		
		if($re)
		{
			while ($row = mysql_fetch_assoc($re))
			{
				$row['title'] 	= stripslashes($row['title']);
				$row['des'] 	= stripslashes($row['des']);
				
				if($row['status'] == 1)
				{
					$row['change'] 	= AZLib::button(Url::build_current(array('cmd'=>'change','id'=>$row['id'],"href"=>urlencode(Url::build_all()))),'style/images/unexam.gif','Disable');
					$row['bgcolor'] = "#FFFFFF";
					$row['status'] = 'OK';
				}
				else
				{
					$row['change'] 	= AZLib::button(Url::build_current(array('cmd'=>'change','id'=>$row['id'],"href"=>urlencode(Url::build_all()))),'style/images/exam.gif','Enable');
					$row['bgcolor'] = "#FF9900";
					$row['status'] = 'Disable';
				}
				
				$row['time_last'] = $row['time_last']?date("H\hi:s d/m/Y",$row['time_last']):'';
				
				$row['time_c'] 	= date("H\hi:s d/m/Y",$row['time_c'])." bởi {$row['user_c']}";
				$row['time_m'] 	= date("H\hi:s d/m/Y",$row['time_m'])." bởi {$row['user_m']}";
				
				$row['edit'] 	= AZLib::button(Url::build_current(array('cmd'=>'edit','id'=>$row['id'],"href"=>urlencode(Url::build_all()))),'style/images/edit.gif','Sửa tài liệu');
					
				$row['delete'] 	= AZLib::buttonDel(Url::build_current(array('cmd'=>'delete','id'=>$row['id'],"href"=>urlencode('?'.$_SERVER['QUERY_STRING']))),'style/images/delete.gif','Sửa tài liệu');
				
				if($row['url'])
				{
					$row['document_detail'] 	= $row['url'] ;
				}
				else
				{
					$row['document_detail'] 	= WEB_DIR."download-{$row['id']}/".AZLib::safe_title($row['title']) . ".html";
					$row['url'] 				= WEB_ROOT."download-{$row['id']}/".AZLib::safe_title($row['title']) . ".html";
				}
				
				$documents[$row['id']] = $row;
			}
		}
		
		$display->add('documents',$documents);
		$display->output('DocumentAdmin');
		$this->endForm();
	}
}

class EditDocumentForm extends Form
{
	var $document =array();
	function __construct()
	{
		Form::__construct('EditDocumentForm');
		
		CGlobal::$website_title = "Sửa tài liệu";
		
		$id = Url::get("id",0);
		
		if($id)
		{
			$this->document = DB::select("document","id=$id");
		}
		
		if(!$this->document)
		{
			Url::redirect_current();
		}
	}

	function on_submit()
	{
		
		$title		=	Url::get('title');

		$des		=	Url::get('des');
		
		$url		=	Url::get('url');
		
		if($des=='<br>') $des = $title;
		
		$this->checkFormInput("Tên tài liệu","title",$title,"str",true,'',1);
		
		$file_path1 = $this->document['file_path'];
		
		if(!$this->errNum)
		{
			if($url != '')
			{
				$file_path1 = '';
			}
			elseif(isset($_FILES['file_path']) && $_FILES['file_path']['error']==0)
			{
				$uploadPath = AZLib::uploadDocPath();
				
				$ext 		= strtolower(AZLib::getExtension($_FILES['file_path']['name']));
				$file_name  = AZLib::safe_title(basename(strtolower($_FILES['file_path']['name']),$ext)).$ext;
				
				$up_file = $uploadPath.date('Y.m.d_H\hi.s')."-{$doc_id}-{$file_name}";
				
				$file_path2 = str_replace(DATA_PATH."document/",'',$up_file);
				
				chmod($uploadPath,0777);
				
				if(!move_uploaded_file( $_FILES['file_path']['tmp_name'], $up_file))
				{
					$this->setFormError('file_path',"Không upload được file!");
					return ;
				}
				else 
				{
					@unlink(CGlobal::$dataCachePath."document/$file_path1");
					
					$file_path1 = $file_path2;
				}
			}
			elseif(!file_exists(DATA_PATH . "document/" . $file_path1))
			{
				$this->setFormError('file_path','Bạn chưa chọn file up hoặc chưa nhập vào Url');
			}
			//update to document
			DB::update('document', array(	
												'title'			=>addslashes($title),
												'des'			=>addslashes($des),
												'file_path'		=>$file_path1,
												'url'			=>$url,
												'time_m'		=>TIME_NOW,
												'user_m'		=>User::user_name(),
												),"id=".$this->document['id']);
			
			
			$href=urldecode(Url::get('href'));
		
			if($href)
			{
				Url::redirect_url($href);
			}
			else
			{
				Url::redirect_current();
			}
			//Url::redirect_current();
		}
	}
	
	function draw()
	{
		global $display;
		$this->beginForm(1);
		
		$display->add("msg",$this->showFormErrorMessages(1));
		$display->add("mod","Sửa");

		$display->add('url_admin',Url::build_current());
		
		$display->add('title',		stripslashes(Url::get('title',$this->document['title'])));
		$display->add('url',		stripslashes(Url::get('url',$this->document['url'])));
		$display->add('des', 		stripslashes(Url::get('des',$this->document['des'])));
		
		$display->output('DocumentAdd');
		$this->endForm();
	}
}


class AddDocumentForm extends Form
{
	function __construct()
	{
		Form::__construct('AddDocumentForm');
		
		CGlobal::$website_title = "Thêm tài liệu";
	}

	function on_submit()
	{
		
		$title		=	Url::get('title');
		$url		=	Url::get('url');
		
		$des		=	Url::get('des');
		
		
		if($des=='<br>') $des = $title;
		
		$this->checkFormInput("Tên tài liệu","title",$title,"str",true,'',1);
		
		if(!$this->errNum)
		{
			$uploadPath = AZLib::uploadDocPath();
			
			//Insert to document
			$doc_id = DB::insert('document', array(	
												'title'			=>addslashes($title),
												'des'			=>addslashes($des),
												'status'		=>1,
												'time_c'		=>TIME_NOW,
												'user_c'		=>User::user_name(),
												
												'time_m'		=>TIME_NOW,
												'user_m'		=>User::user_name(),
												
												'file_path'		=>''
												));
												
												
			if($doc_id)
			{	
				if($url != '')
				{
					DB::update('document', array('file_path'=>'','url'=>$url),"id=$doc_id");
				}
				elseif(isset($_FILES['file_path']) && $_FILES['file_path']['error']==0)
				{
					$ext 		= strtolower(AZLib::getExtension($_FILES['file_path']['name']));
					$file_name  = AZLib::safe_title(basename(strtolower($_FILES['file_path']['name']),$ext)).$ext;
					
					$up_file = $uploadPath.date('Y.m.d_H\hi.s')."-{$doc_id}-{$file_name}";
					
					$file_path = str_replace(DATA_PATH."document/",'',$up_file);
					
					chmod($uploadPath,0777);
					
					if(!move_uploaded_file( $_FILES['file_path']['tmp_name'], $up_file))
					{
						DB::delete('document',"id=$doc_id");
						
						$this->setFormError('file_path',"Không upload được file!");
						return ;
					}
					else
					{
						DB::update('document', array('file_path'=>$file_path),"id=$doc_id");
					}
				}
				
				Url::redirect_current();
				//Url::redirect_current(array('cmd'=>'add'));
			}
			else
			{
				$this->setFormError('name',"Không thêm được tài liệu, hãy thử lại sau!");
			}
		}
	}
	
	function draw()
	{
		global  $display;
		$this->beginForm(1);
		
		$display->add("msg",$this->showFormErrorMessages(1));
		$display->add("mod","Thêm");

		$display->add('url_admin',Url::build_current());
		
		$display->add('title',stripslashes(Url::get('title')));
		$display->add('url',stripslashes(Url::get('url')));
		$display->add('des',stripslashes(Url::get('des')));

		$display->output('DocumentAdd');
		$this->endForm();
	}
}

class Deletedocument extends Form
{
	function __construct()
	{
		$id = Url::get('id');
		
		if($id)
		{
			
			$document=DB::select('document',"id=$id",__LINE__.__FILE__);
			
			if($document)
			{
				$file_src = DATA_PATH."document/{$document['file_path']}";
				
				if(file_exists(ROOT_PATH.$file_src))
				{
					@unlink(ROOT_PATH.$file_src);
				}
				
				DB::delete('document',"id=$id");
			}
		}
		
		$href=urldecode(Url::get('href'));
		
		if($href)
		{
			Url::redirect_url($href);
		}
		else
		{
			Url::redirect_current();
		}
	}
}

class PublicdocumentForm extends Form
{
	function __construct()
	{
		Form::__construct('PublicdocumentForm');
		$id=Url::get('id',0);
		
		if($id)
		{
			
			
			$document=DB::select('document',"id=$id",__LINE__.__FILE__);
			
			if($document)
			{
				DB::update('document', array('status'=>(int)(!$document['status']),'time_m'=>TIME_NOW,'user_m'=>User::user_name()),'id='.$id);
			}
			
		}
		
		if(Url::get('href'))
			Url::redirect_url(urldecode(Url::get('href')));
		else
			Url::redirect_current();
	}
}
?>